package cheme.ProcessEditor.Parts;

import java.util.HashMap;
import java.util.Iterator;

import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.draw2d.Label;
import org.eclipse.draw2d.MouseEvent;
import org.eclipse.draw2d.MouseListener;
import org.eclipse.draw2d.SWTGraphics;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.draw2d.geometry.Rectangle;
import org.eclipse.gef.EditPart;
import org.eclipse.gef.GraphicalEditPart;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Image;

import cheme.ProcessEditor.Figures.AnnotationFig;
import cheme.ProcessEditor.Figures.ConsoleViewFig;
import cheme.ProcessModel.IModelElement;
import cheme.ProcessModel.ProcessModelConstants;
import cheme.ProcessModel.TooltipModel;
import cheme.ProcessModel.Annotations.IAnnotation;

public class AnnotationPart extends PropertyAwarePart
{
	private static HashMap<IModelElement,AnnotationFig> annotationModelMap = new HashMap<IModelElement,AnnotationFig>();
	private  AnnotationFig figure;
	private  AnnotationFig oldfigure;
	public static final Color bgYellow  = new Color(null, 255, 255, 153);
	public static final Color bgWhite  = new Color(null, 255, 255, 255);
	
	protected IFigure createFigure()
	{
		figure = new AnnotationFig();
		//figure.setImage(getModel().getImageDescriptor().createImage());
		// This was setting the toltip.
		//figure.setToolTip(new Label(getModel().getToolTipText()));
		figure.setIndex(getModel().getIndex());
		
		// ading the entry in the map
		annotationModelMap.put(getModel().getParent(),figure);
		
		figure.addMouseListener(new MouseListener()
		{
			public void mousePressed(MouseEvent me)
			{
				String toolTip = getModel().getToolTipText();
				boolean bvisible = true;
				String elementString = getModel().getParent().toString().trim()+ ", ";
								
				bvisible = setBackground(figure,elementString,toolTip);	
				
				Point point = new Point(me.x,me.y);					
				handlemouseClick(point,toolTip, bvisible);	
				// call the console fig function to toggle the b/g color
				String msgCount = figure.getIndex();
				ConsoleViewFig.setConsoleMsgbgColor(msgCount);
			}		

			public void mouseDoubleClicked(MouseEvent me) 
			{
				
			}
			public void mouseReleased(MouseEvent me) {}
		});
		
		return figure;
	}

	// point is the location where tooltip is to be displayed
	public static void handlemouseClick(Point point,String toolTip, boolean bvisible) 
	
	{
			
		TooltipModel model = TooltipModel.getInstance();
		if (model != null)
		{
			model.setvisibility(bvisible);
					
			model.setTipText(toolTip);			
			
			model.setLocation(point);
		}		
	}

	// this function was written to toggle the background color fo the annotation
	public boolean setBackground(AnnotationFig fig, String elementString, String toolTip)
	{
		boolean bVisible  = true;
		togglebgColor(fig);
		if(null == oldfigure)
		{
			oldfigure = figure;		
			//oldfigure.setBackgroundColor(bgYellow);
			// Clicked on a new annotation. log this as message read	        		
    		ProcessModelConstants.log.Write("Read, ",elementString, toolTip);
		}
		// if clicked on the same annotaion figure
		// set the visibility false
		else if(oldfigure == figure)
		{	
			figure.setBackgroundColor(bgWhite);
			oldfigure = null;
			bVisible = false;
		}
		else
		{
			//oldfigure.setBackgroundColor(bgWhite);
			//figure.setBackgroundColor(bgYellow);
			// Clicked on a new annotation. log this as message read					
    		ProcessModelConstants.log.Write("Read, ",elementString, toolTip);	        		
		}
		return bVisible;
	}
	
	public static void togglebgColor(AnnotationFig fig)
	{
		boolean bUniqueFig = true;
		// Go through all the figures
		Iterator It = annotationModelMap.keySet().iterator();
        while (It.hasNext())
        {
        	IModelElement elementkey = (IModelElement)(It.next());  	
            
        	AnnotationFig tempfig = annotationModelMap.get(elementkey);
        	// if bg color of any annotation is yellow, make it white
        	if (tempfig.getBackGroundColor()== bgYellow)
        	{
        		tempfig.setBackgroundColor(bgWhite);
        		//if tempfig and our current fig are same
            	// means clicked twice on the same annotation
        		if(tempfig == fig)
        		{     
        			bUniqueFig = false;
        			break;
        		}
        	}       
         }	
        // if clicked on new annotation
        if(bUniqueFig)
        {
        fig.setBackgroundColor(bgYellow);
        }
		
	}
	
	
	public IAnnotation getModel()
	{
		return (IAnnotation)super.getModel();
	}
	@Override
	protected void createEditPolicies()
	{		
	}
	
	// get the entry from the map
	public static AnnotationFig getAnnotationFig(IModelElement element)
	{
		AnnotationFig fig = null;
		if(null != element && annotationModelMap.containsKey(element))
		{
			fig = (annotationModelMap.get(element));
		}
		return fig;
	}
	
	//remove the entry from the map
	public static void removeAnnotationFigEntry(IModelElement element)
	{
		if(null != element && annotationModelMap.containsKey(element))
		{
			annotationModelMap.remove(element);
		}
	}
			
	protected void refreshVisuals(Point loc)
	{		
		Rectangle constraint = new Rectangle(loc.x, loc.y, -1, -1);		
		
	    ((GraphicalEditPart)getParent()).setLayoutConstraint(this, getFigure(), constraint);
		
	}
}
